package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.promise.jokerdream.database.entity.SectTournamentGameConfigEntity
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.ui.theme.AppSpacing

/**
 * 门派邀请赛设置卡片
 * 包含5个好友设置项，每行两个输入框
 */
@Composable
fun SectTournamentSettingCard(
    config: SectTournamentGameConfigEntity,
    onEnabledChange: (Boolean) -> Unit,
    onSetting1ValueChange: (String) -> Unit,
    onSetting2ValueChange: (String) -> Unit,
    onSetting3ValueChange: (String) -> Unit,
    onSetting4ValueChange: (String) -> Unit,
    onSetting5ValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    SettingCard(
        title = "门派邀请赛",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onEnabledChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
                ) {
                    // 第一行：好友1 和 好友2
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
                    ) {
                        // 好友1
                        InputField(
                            title = "好友1",
                            value = config.setting1Value,
                            onValueChange = onSetting1ValueChange,
                            modifier = Modifier.weight(1f)
                        )
                        
                        // 好友2
                        InputField(
                            title = "好友2",
                            value = config.setting2Value,
                            onValueChange = onSetting2ValueChange,
                            modifier = Modifier.weight(1f)
                        )
                    }
                    
                    // 第二行：好友3 和 好友4
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
                    ) {
                        // 好友3
                        InputField(
                            title = "好友3",
                            value = config.setting3Value,
                            onValueChange = onSetting3ValueChange,
                            modifier = Modifier.weight(1f)
                        )
                        
                        // 好友4
                        InputField(
                            title = "好友4",
                            value = config.setting4Value,
                            onValueChange = onSetting4ValueChange,
                            modifier = Modifier.weight(1f)
                        )
                    }
                    
                    // 第三行：好友5
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
                    ) {
                        // 好友5
                        InputField(
                            title = "好友5",
                            value = config.setting5Value,
                            onValueChange = onSetting5ValueChange,
                            modifier = Modifier.weight(1f)
                        )
                        
                        // 占位空间
                        Spacer(modifier = Modifier.weight(1f))
                    }
                }
            }
        }
    )
}

/**
 * 输入框组件 - 左文案右输入框
 * 用于设置每个好友的数值
 */
@Composable
private fun InputField(
    title: String,
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        // 标题 - 优先显示
        Text(
            text = title,
            fontSize = AppFontSizes.mediumBody,
            color = MaterialTheme.colorScheme.onSurface
        )
        
        // 数字输入框 - 占剩余空间
        Box(
            modifier = Modifier
                .weight(1f)
                .height(24.dp)
                .background(
                    color = MaterialTheme.colorScheme.surface,
                    shape = RoundedCornerShape(4.dp)
                )
                .border(
                    width = 1.dp,
                    color = MaterialTheme.colorScheme.outline,
                    shape = RoundedCornerShape(4.dp)
                )
                .padding(horizontal = 4.dp, vertical = 2.dp),
            contentAlignment = Alignment.Center
        ) {
            BasicTextField(
                value = value,
                onValueChange = { newValue: String ->
                    // 只允许输入数字
                    if (newValue.all { char: Char -> char.isDigit() }) {
                        onValueChange(newValue)
                    }
                },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                singleLine = true,
                textStyle = androidx.compose.ui.text.TextStyle(
                    fontSize = 12.sp,
                    color = androidx.compose.ui.graphics.Color.Black,
                    textAlign = androidx.compose.ui.text.style.TextAlign.Center
                ),
                decorationBox = { innerTextField ->
                    innerTextField()
                }
            )
        }
    }
}
